//
//  XSM3u8TableViewCell.m
//  XSDownloadDemo
//
//  Created by 大帅 on 2024/1/5.
//

#import "XSM3u8TableViewCell.h"

@interface XSM3u8TableViewCell ()
/** 视频名称 */
@property (nonatomic, strong) UILabel *videoLabel;
/** 视频下载大小信息 */
@property (nonatomic, strong) UILabel *sizeLabel;
/** 视频下载速度 */
@property (nonatomic, strong) UILabel *speedLabel;
/** 视频下载进度条 */
@property (nonatomic, strong) UIProgressView *progressView;

@end

@implementation XSM3u8TableViewCell

-(UILabel *)videoLabel {
    if (!_videoLabel) {
        _videoLabel = [[UILabel alloc] init];
        _videoLabel.textColor = UIColor.blackColor;
        _videoLabel.font = [UIFont systemFontOfSize:16];
    }
    return _videoLabel;
}

-(UIProgressView *)progressView {
    if (!_progressView) {
        _progressView = [[UIProgressView alloc] init];
        _progressView.progressTintColor = UIColor.blackColor;
        _progressView.trackTintColor = UIColor.greenColor;
        _progressView.layer.cornerRadius = 1;
        _progressView.layer.masksToBounds = YES;
    }
    return _progressView;
}

-(UILabel *)sizeLabel {
    if (!_sizeLabel) {
        _sizeLabel = [[UILabel alloc] init];
        _sizeLabel.textColor = UIColor.blackColor;
        _sizeLabel.font = [UIFont systemFontOfSize:13];
    }
    return _sizeLabel;
}

-(UILabel *)speedLabel {
    if (!_speedLabel) {
        _speedLabel = [[UILabel alloc] init];
        _speedLabel.textColor = UIColor.blackColor;
        _speedLabel.font = [UIFont systemFontOfSize:13];
    }
    return _speedLabel;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        //self.selectionStyle = UITableViewCellSelectionStyleNone;
        // cell选中状态背景
        UIView *cell_bjv = [[UIView alloc] init];
        cell_bjv.backgroundColor = [UIColor.cyanColor colorWithAlphaComponent:0.2];
        self.selectedBackgroundView = cell_bjv;
        
        [self createViewUI];
    }
    return self;
}

-(void)createViewUI {
    [self.contentView addSubview:self.videoLabel];
    [self.videoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(10);
        make.height.mas_equalTo(22);
    }];
    
    [self.contentView addSubview:self.sizeLabel];
    [self.sizeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.videoLabel);
    }];
    
    [self.contentView addSubview:self.progressView];
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(self.videoLabel.mas_bottom).offset(5);
        make.height.mas_equalTo(2);
        make.right.mas_equalTo(-15);
    }];
    
    [self.contentView addSubview:self.speedLabel];
    [self.speedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(self.progressView.mas_bottom).offset(5);
        make.height.mas_equalTo(17);
    }];
        
    
}

-(void)setModel:(XSTestModel *)model {
    _model = model;
    self.videoLabel.text = model.videoName;
    self.progressView.progress = model.progress;
    self.sizeLabel.text = [NSString stringWithFormat:@"%@M / %@M", [self stringFromByteCount:model.downloadSizeM3u8], [self stringFromByteCount: model.totalSizeM3u8]];
        
    switch (model.state) {
        case XSDownloadStatusDownloading:
            // 下载中
            self.speedLabel.text = [NSString stringWithFormat:@"%@M/s", [self stringFromByteCount:model.speed]];
            self.progressView.progressTintColor = UIColor.redColor;
            break;
            
        case XSDownloadStatusWaiting:
            self.speedLabel.text = @"等待中";
            self.progressView.progressTintColor = UIColor.blackColor;
            break;
            
        case XSDownloadStatusFinish:
            self.speedLabel.text = @"已完成";
            self.progressView.progressTintColor = UIColor.redColor;
            break;
            
        case XSDownloadStatusPause:
            self.speedLabel.text = @"已暂停";
            self.progressView.progressTintColor = UIColor.blackColor;
            break;
            
        case XSDownloadStatusError:
            self.speedLabel.text = @"下载错误";
            self.progressView.progressTintColor = UIColor.blackColor;
            break;
            
        default:
            self.sizeLabel.text = @"";
            self.speedLabel.text = @"";
            self.progressView.progressTintColor = UIColor.blackColor;
            break;
    }
}

// 根据字节大小返回文件大小字符KB、MB
- (NSString *)stringFromByteCount:(NSInteger)byteCount {
    return [NSString stringWithFormat:@"%.2f", byteCount/1024.0/1024.0];
}

- (NSString *)convertSpeedSize:(long long)size {
    long kb = 1024;
    long mb = kb * 1024;
    long gb = mb * 1024;
    
    if (size >= gb) {
        return [NSString stringWithFormat:@"%.1fGB/s", (float) size / gb];
    } else if (size >= mb) {
        float f = (float) size / mb;
        if (f > 100) {
            return [NSString stringWithFormat:@"%.0fMB/s", f];
        }else{
            return [NSString stringWithFormat:@"%.1fMB/s", f];
        }
    } else if (size >= kb) {
        float f = (float) size / kb;
        if (f > 100) {
            return [NSString stringWithFormat:@"%.0fKB/s", f];
        }else{
            return [NSString stringWithFormat:@"%.1fKB/s", f];
        }
    } else
        return [NSString stringWithFormat:@"%lldB/s", size];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
